import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import * as fileApi from '@/api/fileApi'
import {ElMessage, ElNotification} from "element-plus";

export const usePreviewZipStore = defineStore('previewZipStore', () => {

  const show = ref(false)
  const absolutePath = ref('')
  const title = ref('')
  const fileList = ref([])
  const curPath = ref([''])
  async function zip(directoryName) {
    let res = await fileApi.previewZip(absolutePath.value,directoryName)
    fileList.value = res.data
    if(directoryName) {
      curPath.value.push(directoryName)
      // 添加第一个元素
      fileList.value.unshift({
        name: "..",
        absolutePath: directoryName
      })
    }
  }

  async function back() {
    if(curPath.value.length === 0) return
    curPath.value.pop()
    let res = await fileApi.previewZip(absolutePath.value,curPath.value[curPath.value.length - 1])
    fileList.value = res.data
    if(curPath.value.length > 1) {
      fileList.value.unshift({
        name: "..",
        absolutePath: curPath.value[curPath.value.length - 1]
      })
    }
  }

  return {show,zip,absolutePath,title,fileList,back,curPath}

})
